"use strict";

/**
 * author:Administrator
 * date:2016-01-12
 * desc: 工具类
 * 日志
 * 格式输出规范
 * 日期格式规范
 */
//var errCodes = require('./err_code.js');
var __utils = {};
module.exports = __utils;

/**************************************/
/*                 日志           *****/
/**************************************/
var config = require('../config/index.js');
var log4js = require('log4js');
log4js.configure(config.log4js);
var loggerinfo = log4js.getLogger('info');
var loggererror = log4js.getLogger('error');
var loggerdebug = log4js.getLogger('debug');
//loggerinfo.info('This is Information Logger');
//loggererror.info('This is Error Logger');
//loggerdebug.info('This is Debugger');
//__utils.logInfo = loggerinfo.info.bind(loggerinfo);
//__utils.logError = loggererror.info.bind(loggererror);
//__utils.logDebug = loggerdebug.info.bind(loggerdebug);
__utils.logInfo = function () {
  if (!config.nodeEnv != 'production') {
    console.log.apply(console.log, arguments);
  }
  loggerinfo.info.apply(loggerinfo, arguments);
};
__utils.logError = function () {
  if (!config.nodeEnv != 'production') {
    console.log.apply(console.log, arguments);
  }
  loggererror.info.apply(loggerinfo, arguments);
};
__utils.logDebug = function () {
  if (!config.nodeEnv != 'production') {
    console.log.apply(console.log, arguments);
  }
  loggerdebug.info.apply(loggerinfo, arguments);
};

/**************************************/
/*               格式输出/错误      *****/
/**************************************/

function __ResultObject(errCode, data, msg) {
  this.errcode = errCode;
  this.data = data;
  this.msg = msg;
}
__ResultObject.prototype.isOk = function () {
  return this.errcode == 0;
};
__ResultObject.prototype.getData = function () {
  return this.data;
};
__ResultObject.prototype.getMsg = function () {
  return this.msg;
};

__utils.fout = function (data, msg) {
  if (data instanceof __ResultObject) return data;
  return new __ResultObject(0, data, msg);
};

__utils.ferror = function (errObj, extraMsg) {
  if (errObj instanceof  __ResultObject) {
    if (!!extraMsg)errObj.msg = errObj.msg + ' - ' + extraMsg;
    return errObj;
  }
  var msg = extraMsg ? (errObj.msg + ' - ' + extraMsg) : errObj.msg;
  return new __ResultObject(errObj.code, null, msg);
};

/**************************************/
/*    日期格式 yyyy-MM-dd HH:mm:ss *****/
/**************************************/
var __DATE_FORMAT = 'yyyy-MM-dd HH:mm:ss';
function __keep2(digit) {
  var tmp = '00' + digit;
  return tmp.substr(tmp.length - 2, 2);
}
__utils.fmtDate = function (date) {
  date = date || new Date();
  return __DATE_FORMAT
    .replace('yyyy', date.getFullYear())
    .replace('MM', __keep2(date.getMonth() + 1))
    .replace('dd', __keep2(date.getDate()))
    .replace('HH', __keep2(date.getHours()))
    .replace('mm', __keep2(date.getMinutes()))
    .replace('ss', __keep2(date.getSeconds()));
};


/**************************************/
/*              md5工具                */
/**************************************/

var crypto = require('crypto');
/**
 * md5摘要
 * @param str
 */
__utils.md5 = function (str) {
  return crypto.createHash('md5').update(str).digest('hex');
};

